/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IR;

import java.io.File;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.FieldInfo.IndexOptions;

/**
 *
 * @author Shadownest
 */
public class NewsArticleDocument {
    Field Name;
    Field Owner;
    Field Contents;
    NumericField Modified;
    Field Path;

    public NewsArticleDocument(File file) throws IOException {
        FileInputStream fis = new FileInputStream(file);
        BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
        String line = reader.readLine();
        String content = "";
        while (line != null && !line.isEmpty()) {
            if (line.startsWith("Subject: ")) {
                Name = new Field("name", line.substring(9), Field.Store.YES, Field.Index.ANALYZED);
                Name.setIndexOptions(IndexOptions.DOCS_ONLY);
            }
            else if (line.startsWith("From: ")) {
                Owner = new Field("owner", line.substring(6), Field.Store.YES, Field.Index.ANALYZED);
                Owner.setIndexOptions(IndexOptions.DOCS_ONLY);
            }
            else
                content += line + "\n";
            line = reader.readLine();
        }
        while (line != null) {
            content += line + "\n";
            line = reader.readLine();
        }
        Contents = new Field("contents", content, Field.Store.YES, Field.Index.ANALYZED);
        Contents.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);

        Modified = new NumericField("modified");
        Modified.setLongValue(file.lastModified());

        Path = new Field("path", file.getPath(), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
        Path.setIndexOptions(IndexOptions.DOCS_ONLY);
}
    
}
